import api from '@/api/exam'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 1,
    filter: {
        enabled: null,
        classIds: null,
        name: null
    }
}

const state = {
    data: {
        paperList: [],
        evidences: []
    },
    ui: {
        page: deepCopy(initPage)
    }
}

// getters
const getters = {
    paperList: state => state.data.paperList
}

// actions
// 分页查询培训任务
const actions = {
    GetPaperList({ commit }, data) {
        api.getstudyPaper(data ? data : state.ui.page, {
            loading: true,
            success: res => {
                const { pages, total } = res
                commit('SETPAPERPAGE', { pages, total })
                commit('SetPaperData', res)
            }
        })
    },
    // 删除某一条数据
    RemoveOneData({ dispatch }, data) {
        api.removeonePaper(data, {
            success: () => {
                dispatch('GetPaperList')
            }
        })
    },
    // 添加试卷
    AddPage({ dispatch }, data) {
        api.createPaper(data.data, {
            success: res => {
                dispatch('GetPaperList')
                data.cb && data.cb(res)
            }
        })
    },
    // 根据ID获取任务列表
    QueryFindPage({ commit }, id) {
        api.queryOnePaper(id, {
            success: res => {
                commit('SetPaperData', res)
            }
        })
    },
    // 根据ID更新任务
    UpdateOnePage({ dispatch }, id) {
        api.updateOnePage(id, {
            success: res => {
                dispatch('GetPaperList')
            }
        })
    },
    // 保存更新试卷和业务的关系
    saveExamPaperBusinessScores( { dispatch }, examPaperBusinessScores) {
        api.saveExamPaperBusinessScores(examPaperBusinessScores)
    }
}

// mutations
const mutations = {
    SetPaperData: (state, payload) => {
        state.data.paperList = payload
    },
    SETPAPERPAGE(state, page) {
        page && setPage(state.ui.page, page)
    },
    setEvidences: (state, payload) => {
        state.data.evidences = payload.content
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
